package ar.com.ctrln.view.dashboard.compras.proveedor;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.EventQueue;
import org.zkoss.zk.ui.event.EventQueues;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.VariableResolver;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zk.ui.select.annotation.WireVariable;
import org.zkoss.zul.Footer;
import org.zkoss.zul.Grid;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Textbox;

import ar.com.ctrln.control.compras.proveedor.ConsultarProveedor;
import ar.com.ctrln.persistence.model.compra.PedidoCompra;
import ar.com.ctrln.persistence.model.proveedor.Proveedor;
import ar.com.ctrln.persistence.model.ticket.actividad.DetalleActividad;
import ar.com.ctrln.view.ui.comportamientoVentana.AbstractMinimizarVentana;

@VariableResolver(org.zkoss.zkplus.spring.DelegatingVariableResolver.class)
public class ConsultarProveedorController extends AbstractMinimizarVentana {

	private static final long serialVersionUID = -5026484941900569851L;

	@WireVariable
	private ConsultarProveedor consultarProveedor;
		
	private List<Proveedor> proveedores;

	private EventQueue eqA = (EventQueue) EventQueues.lookup("refreshProveedores",EventQueues.DESKTOP, true);
	
	@Wire
	private Grid clientesGrid;

	@Wire("#txtFilter")
	private Textbox txtFilter;

	@Wire
	private Footer footer_msg;

	@Wire
	private Footer footer_category;

	@SuppressWarnings("unchecked")
	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		clientesGrid.setModel(getConsultaProveedor());
		eqA.subscribe(new EventListener<Event>() {
			public void onEvent(Event event) throws Exception {
				consultarProveedor.actualizarProveedor((Proveedor)event.getData());
				clientesGrid.setModel(getConsultaProveedor());
				System.out.println("ACTUALIZOOOOOOOOOOOOOOOOOOOOO");
			}});
	}

	public ListModel<Proveedor> getConsultaProveedor() {
		proveedores = consultarProveedor.listaProveedores();
		return new ListModelList<Proveedor>(proveedores);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Listen("onChanging = #txtFilter")
	public void getFilter(Event event) {
		((Textbox) event.getTarget())
				.setValue(((org.zkoss.zk.ui.event.InputEvent) event).getValue());
		clientesGrid.setModel(new ListModelList(getFilterBy(txtFilter.getValue(),proveedores)));
		if (clientesGrid.getRows().getChildren().size() > 0) {
			footer_msg.setLabel("");
			footer_category
					.setLabel("Un total de "
							+ clientesGrid.getRows().getChildren().size()
							+ " Proveedores");
		} else { 
			footer_msg.setLabel("Ningun Resultado...");
			footer_category.setLabel("");
		}
		((Textbox) event.getTarget()).setFocus(true);
	}

	public static List<Proveedor> getFilterBy(String condicion, Collection<Proveedor> proveedores) {
		List<Proveedor> data = new ArrayList<Proveedor>();
		for (Iterator<Proveedor> i = proveedores.iterator(); i.hasNext();) {
			Proveedor tmp = i.next();
			if (tmp.getRazonSocial().toLowerCase()
					.indexOf(condicion.trim().toLowerCase()) >= 0
					|| tmp.getTipoServicios().iterator().next().getNombre().toLowerCase()
							.indexOf(condicion.trim().toLowerCase()) >= 0) {
				data.add(tmp);
			}
		}
		return data;
	}
}

